package cn.wolfcode.shiro;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2020/5/18.
 */
@Component
public class CrmRealm extends AuthorizingRealm {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //通过token获取用户名(用户登录的时候填的)
//        token.getCredentials();
        Object username = token.getPrincipal();
        //判断是否存在数据库
        Employee employee = employeeMapper.selectByName(username.toString());
        if(employee!=null){
            //身份信息,凭证信息,只有一个数据源,realm
            return new SimpleAuthenticationInfo(employee,employee.getPassword(),
                    ByteSource.Util.bytes(username),"CrmRealm");
        }
        //返回值就是查询出来的数据,如果查到这个账号,就应该返回该账号的正确的数据,若查不到返回空
        return null;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("--------------------------------");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        //获取读取的身份信息
        Employee employee = (Employee)subject.getPrincipal();
//        System.out.println(employee);
        //判断是否是admin
        if(employee.isAdmin()){
            info.addRole("admin");
            info.addStringPermission("*:*");
        }else{
            //根据用户的id查询用户拥有的角色
            List<Role> roles = roleMapper.selectByEmpId(employee.getId());
            ArrayList<String> rolelist = new ArrayList<>();
            for (Role role:roles){
                rolelist.add(role.getSn());
            }
            info.addRoles(rolelist);
            //根据用户的id查询用户拥有的权限
            List<String> permissions = permissionMapper.selectByEmpId(employee.getId());
            info.addStringPermissions(permissions);
        }
        //当前登录用户所有的角色
//        info.addRole("HR_MGR");
//        info.addStringPermissions(Arrays.asList("employee:update","employee:delete"));
        return info;
    }
}
